package com.zy.explore.wechat.assembler;

import com.zy.explore.wechat.domain.mybatis.entity.MybatisExampleEntity;
import com.zy.explore.wechat.frm.ExampleFrm;
import com.zy.explore.wechat.frm.SortFieldFrm;
import com.zy.explore.wechat.vo.ExampleVO;
import com.zy.explore.wechat.util.DataBeanUtil;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * description Mybatis示例Assembler
 *
 * @author ZY
 * @date 2021年05月15日 11:23
 */
public class MybatisExampleAssembler {

    private MybatisExampleAssembler() {}

    /**
     * Frm 转 实体类
     *
     * @param frm 参数对象
     * @return 返回转换后的实体对象
     */
    public static MybatisExampleEntity convertFromFrm(ExampleFrm frm) {
        MybatisExampleEntity mybatisExampleEntity = DataBeanUtil.copyOf(frm, MybatisExampleEntity.class, true);
        if (!StringUtils.isEmpty(frm.getId())) {
            mybatisExampleEntity.setId(Integer.parseInt(frm.getId()));
        }
        return mybatisExampleEntity;
    }

    /**
     * 实体类 转 VO
     *
     * @param entity 实体类对象
     * @return 返回转化后的VO对象
     */
    public static ExampleVO convertToVO(MybatisExampleEntity entity) {
        ExampleVO exampleVO = DataBeanUtil.copyOf(entity, ExampleVO.class, true);
        if (entity.getId() != null) {
            exampleVO.setId(entity.getId() + "");
        }
        return exampleVO;
    }

    /**
     * Frm 转 Example
     *
     * @param frm 参数对象
     * @return 返回Example对象
     */
    public static Example convertToExample(ExampleFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        Example example = new Example(MybatisExampleEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(frm.getId())) {
            criteria.andEqualTo("id", Integer.parseInt(frm.getId()));
        }
        if (!StringUtils.isEmpty(frm.getName())) {
            criteria.andLike("name", "%" + frm.getName() + "%");
        }
        if (!StringUtils.isEmpty(frm.getAddress())) {
            criteria.andLike("address", "%" + frm.getAddress() + "%");
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("createTime", frm.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", frm.getCreateTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
            criteria.andGreaterThanOrEqualTo("updateTime", frm.getUpdateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
            criteria.andLessThanOrEqualTo("updateTime", frm.getUpdateTimeEnd());
        }
        // 排序
        List<SortFieldFrm> sortFields = frm.getSortFields();
        if (!CollectionUtils.isEmpty(sortFields)) {
            sortFields.forEach(sortField -> {
                if (Sort.Direction.ASC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).asc();
                } else if (Sort.Direction.DESC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).desc();
                }
            });
        } else {
            // 默认按主键降序排序
            example.orderBy("id").desc();
        }
        return example;
    }
}
